//
//  HMProgressView.swift
//  HMWeiBo
//
//  Created by Apple on 16/8/10.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class HMProgressView: UIImageView {

    var gro: CGFloat = 0 {
        
        didSet{
            
            loadingView.progress = gro
            
        }
    }
    
    // 第一步: 重写父类的构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 第二步: 定义一个添加子类和设置子类控件约束的方法
    private func setupUI(){
        
        addSubview(loadingView)
        
        loadingView.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(self.snp_center)
            // 注意 图片的约束
            make.edges.equalTo(self.snp_edges)
        }
        
//        loadingView.startAnimating()
        
    }
    
    //第三步: 懒加载控件
    //MARK:-- 懒加载控件
//    private lazy var loadingView: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
    
    private lazy var loadingView: ITHMProgressView = {
    
        let pro = ITHMProgressView()
    
        
        return pro
        
    }()

}
class ITHMProgressView: UIView {
    
    var progress: CGFloat = 0{
    
        didSet{
            
        
            setNeedsDisplay()
        }
        
    }
    
    override func drawRect(rect: CGRect) {
        /*
            1. center 原点
            2. radius 半径
            3. 起始的角度
            4. 结束的角度
            5. 顺时针 逆时针
        */
        
        let center = CGPointMake(rect.width * 0.5, rect.height * 0.5)
        
        let radius = min(rect.width, rect.height) * 0.5
        
        let startAngle: CGFloat = 0
        let endAngle: CGFloat =  progress * 2 * CGFloat(M_PI)
        
        
        // 路径
        let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        
        // 添加中心
        path.addLineToPoint(center)
        
        // 关闭
        path.closePath()
        
        // 设置颜色
        UIColor.redColor().setFill()
        
        // 填充/画
        path.fill()
        
    }
    
}
